package com.invengo.module_check.ui.fragment.first_check;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.invengo.library_db.entity.module_check.RecordBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;
import com.invengo.module_check.ui.fragment.first_check.item.PosRecordItemViewModel;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class RecordViewModel extends BaseViewModel<BusinessRepsitory> {
    private String fragment = "RecordRefreshFragment";
    public String finishTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 条";
    public String refreshTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 条";
    public ObservableList<PosRecordItemViewModel> refreshList = new ObservableArrayList<>();
    public ItemBinding<PosRecordItemViewModel> refreshItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_record_refreshing);
    public SingleLiveEvent<Boolean> isRefreshLoadmore = new SingleLiveEvent<>();

    public ObservableList<PosRecordItemViewModel> finishList = new ObservableArrayList<>();
    public ItemBinding<PosRecordItemViewModel> finishItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_record_finish);

    public SingleLiveEvent<Boolean> isFinishLoadmore = new SingleLiveEvent<>();

    public RecordViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
        isFinishLoadmore.postValue(true);
        isRefreshLoadmore.postValue(true);
    }

    private int status = 2;
    public static final int PAGE_SIZE = 20;
    private int page_refresh = 1;
    private int page_finish = 1;

    public UIChangeObservable ui = new UIChangeObservable();


    public class UIChangeObservable {
        public SingleLiveEvent<String> finishTipEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<String> refreshTipEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> finishLoad = new SingleLiveEvent<>();
    }

    //0 未完成 1已完成 2查询全部
    public void getRefreshRecord() {
        model.getUpdateRecordList(0, page_refresh, PAGE_SIZE)
                .compose(RxUtils.applySchedulersLoading(getLifecycleProvider(), this))
                .subscribe(recordBean -> {
                    if (page_refresh == 1)
                        refreshList.clear();
                    if (recordBean.getItems().size() < PAGE_SIZE)
                        isRefreshLoadmore.postValue(false);
                    for (RecordBean.ItemsDTO item : recordBean.getItems()) {
                        refreshList.add(new PosRecordItemViewModel(this, item));
                    }
                    ui.refreshTipEvent.postValue(String.format(refreshTip, recordBean.getTotalCount()));
                    ui.finishLoad.call();
                });
    }

    //0 未完成 1已完成 2查询全部
    public void getFinishRecord() {
        model.getUpdateRecordList(1, page_finish, PAGE_SIZE)
                .compose(RxUtils.applySchedulersLoading(getLifecycleProvider(), this))
                .subscribe(recordBean -> {
                    if (page_finish == 1)
                        finishList.clear();
                    if (recordBean.getItems().size() < PAGE_SIZE)
                        isFinishLoadmore.postValue(false);
                    for (RecordBean.ItemsDTO item : recordBean.getItems()) {
                        finishList.add(new PosRecordItemViewModel(this, item));
                    }
                    ui.finishTipEvent.postValue(String.format(finishTip, recordBean.getTotalCount()));
                    ui.finishLoad.call();
                });
    }


    public int getPosition(ItemViewModel item) {
        switch (fragment) {
            case "RecordRefreshFragment":
                return refreshList.indexOf(item);
            case "RecordFinishFragment":
                return finishList.indexOf(item);
        }
        return 0;
    }

    public BindingCommand loadMore = new BindingCommand(() -> {
        switch (fragment) {
            case "RecordRefreshFragment":
                page_refresh += 1;
                getRefreshRecord();
                break;
            case "RecordFinishFragment":
                page_finish += 1;
                getFinishRecord();
                break;
        }
    });

    public BindingCommand refresh = new BindingCommand(() -> {
        switch (fragment) {
            case "RecordRefreshFragment":
                page_refresh = 1;
                getRefreshRecord();
                break;
            case "RecordFinishFragment":
                page_finish = 1;
                getFinishRecord();
                break;
        }
    });


    public void setFragment(String fragment) {
        this.fragment = fragment;
    }
}
